package com.client.ryt.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.client.ryt.R;
import com.client.ryt.basic.BaseActivity;
import com.client.ryt.basic.BaseException;
import com.client.ryt.basic.BaseLiteners;
import com.client.ryt.basic.BaseResponse;
import com.client.ryt.basic.BaseTask;
import com.client.ryt.config.LoginConfig;
import com.client.ryt.entity.parameter.CheckCardParameters;
import com.client.ryt.entity.parameter.TradeParamters;
import com.client.ryt.task.http.request.CheckCardRequest;
import com.client.ryt.task.http.request.TradeInfoRequest;
import com.client.ryt.task.http.request.TradeRequest;
import com.client.ryt.task.http.response.CheckCardResponse;
import com.client.ryt.task.http.response.TradeInfoResponse;
import com.client.ryt.widget.scaner.MipcaActivityCapture;
import com.client.ryt.widget.zxing.activity.CaptureActivity;

public class CashDoneActivity extends BaseActivity {

    private final static int SCANNIN_GREQUEST_CODE = 1;
    TextView tv_shopname;
    TextView tv_followname;
    TextView tv_tradeitem;
    TextView tv_tradedesc;

    Button btn_trade;
    private String card_sn;
    CaptureCancelReciever captureCancelReciever = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        captureCancelReciever = new CaptureCancelReciever();
        IntentFilter filter = new IntentFilter();
        filter = new IntentFilter();
        filter.addAction("capturecancel");
        context.registerReceiver(captureCancelReciever, filter);
        startCash();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.menu_cash_done, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /*************
     * 验证卡
     * @param
     */
    private void checkCard(){
        CheckCardParameters checkCardParameters= new CheckCardParameters();
        checkCardParameters.setToken(LoginConfig.getLoginConfig().getToken());
        checkCardParameters.setUsername(LoginConfig.getLoginConfig().getShopMember().getUsername());
        checkCardParameters.setShopid(LoginConfig.getLoginConfig().getShopMember().getShop_id());
        checkCardParameters.setCardsn(card_sn);
        new BaseTask<CheckCardRequest,CheckCardResponse>(
                CashDoneActivity.this,CheckCardRequest.class,CheckCardResponse.class,checkCardParameters,new BaseLiteners.BaseResponseListener() {
            @Override
            public void OnFail(BaseException e) {
                //showToast(e.getErrmsg());
                Intent intent = new Intent(CashDoneActivity.this,CashCodeActivity.class);
                intent.putExtra("error_msg",e.getErrmsg());
                startActivity(intent);
                finish();
            }

            @Override
            public void OnSuccess(BaseResponse o) {
                if(o.getIsSuccess()){
                    //todo
                    getTradeInfo();
                }
            }
        },false).execute();
    }

    /*********
     * 扫描回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SCANNIN_GREQUEST_CODE:
                if(resultCode == RESULT_OK){
                    Bundle bundle = data.getExtras();
                    //显示扫描到的内容
                    card_sn = bundle.getString("result");
                    checkCard();
                }
                break;
        }
    }

    /***
     * 调用扫描组件
     */
    private void startCash(){
        Intent intent = new Intent();
        intent.setClass(CashDoneActivity.this, CaptureActivity.class);
       // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
    }


    /************
     * 获取交易信息
     */
    private void getTradeInfo(){
        final TradeParamters tradeParamters = new TradeParamters();
        tradeParamters.setToken(LoginConfig.getLoginConfig().getToken());
        tradeParamters.setUsername(LoginConfig.getLoginConfig().getShopMember().getUsername());
        tradeParamters.setShopid(LoginConfig.getLoginConfig().getShopMember().getShop_id());
        tradeParamters.setCardsn(card_sn);

        new BaseTask<TradeInfoRequest,TradeInfoResponse>(this,TradeInfoRequest.class,TradeInfoResponse.class,tradeParamters,new BaseLiteners.BaseResponseListener() {
            @Override
            public void OnFail(BaseException e) {
                Intent intent = new Intent(CashDoneActivity.this,CashCodeActivity.class);
                intent.putExtra("error_msg",e.getErrmsg());
                startActivity(intent);
                finish();
            }

            @Override
            public void OnSuccess(BaseResponse o) {
                if(o.getIsSuccess()){
                    setContentView(R.layout.activity_cash_done);
                    tv_shopname = (TextView)findViewById(R.id.tv_shopname);
                    tv_followname = (TextView)findViewById(R.id.tv_followname);
                    tv_tradeitem = (TextView)findViewById(R.id.tv_tradeitme);
                    tv_tradedesc = (TextView)findViewById(R.id.tv_tradedesc);
                    btn_trade = (Button)findViewById(R.id.btn_trade);
                    btn_trade.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            final TradeParamters tradeParamters = new TradeParamters();
                            tradeParamters.setToken(LoginConfig.getLoginConfig().getToken());
                            tradeParamters.setUsername(LoginConfig.getLoginConfig().getShopMember().getUsername());
                            tradeParamters.setShopid(LoginConfig.getLoginConfig().getShopMember().getShop_id());
                            tradeParamters.setCardsn(card_sn);
                            new BaseTask<TradeRequest,BaseResponse>(CashDoneActivity.this,TradeRequest.class,BaseResponse.class,tradeParamters,new BaseLiteners.BaseResponseListener() {
                                @Override
                                public void OnFail(BaseException e) {
                                    showToast(e.getErrmsg());
                                }

                                @Override
                                public void OnSuccess(BaseResponse o) {
                                    showToast("消费成功");
                                    finish();
                                }
                            },true).execute();
                        }
                    });


                    TradeInfoResponse tradeInfoResponse = (TradeInfoResponse)o;
                    tv_shopname.setText(tradeInfoResponse.getShopname());
                    tv_followname.setText(tradeInfoResponse.getFollowname());
                    tv_tradeitem.setText(tradeInfoResponse.getTradeitem());
                    tv_tradedesc.setText(tradeInfoResponse.getTradedesc());
                }
            }
        },true).execute();
    }

    public class CaptureCancelReciever extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            CashDoneActivity.this.finish();
        }
    }
}
